package com.bonc.rms.controller.tms;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.rms.entity.tms.Template;
import com.bonc.rms.service.tms.ZEmployeeService;

/**
 * ZEmployeeController
 * @author zlq
 *
 */

@Controller
@RequestMapping(value = "/show")
public class ZEmployeeController {

	@Autowired
	private ZEmployeeService zEmployeeService;
	
	
	/**
	 * 人员按学历和部门分布
	 * @return
	 */	
	@ResponseBody
	@RequestMapping(value = "/degree")
	public String getDegreeDistribute(Integer departmentId){
		
		if (departmentId == null || "".equals(departmentId)) {
			departmentId = 1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getDegreeDistribute(departmentId);
		map.put("title", "学历分布");
		map.put("degree", list);
		return JSON.toJSONString(map);
	}
	
	/**
	 * 人员按性别和部门分布
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/gender")
	public String getGenderDistribute(Integer departmentId){
		
		if (departmentId == null || "".equals(departmentId)) {
			departmentId = 1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getGenderDistribute(departmentId);
		map.put("title","性别分布");
		map.put("gender", list);
		return JSON.toJSONString(map);
	}
	
	/**
	 * 年龄分布
	 * @return
	 */
	/**
	 * 

	@ResponseBody
	@RequestMapping(value = "/age")
	public String getAgeDistribute(){
		
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getAgeDistribute();
		map.put("title", "年龄分布");
		map.put("age", list);
		return JSON.toJSONString(map);
	}
	 * @return
	 */
	/**
	 * 工作年限分布
	 * @return
	 */
	/**
	 * 

	@ResponseBody
	@RequestMapping(value = "/workExperience")
	public String getEntryType(){
		
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getWorkExperienceDistribute();
		map.put("title", "司龄分布");
		map.put("workExperience", list);
		return JSON.toJSONString(map);	
		
	}
	 * @return
	 */
	/**
	 * HC指标
	 * @param departmentId
	 * @param startMonth
	 * @param endMonth
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/entryType")
	public String getworkExperienceDistribute(Integer departmentId, String startTime, String endTime){
		
		if (departmentId == null) {
			departmentId = 1;
		}
		// 开始时间、结束时间任一为空
		// 则默认显示今年的数据
		if( null == startTime || "".equals(startTime) || null == endTime || "".equals(endTime)) {
			String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
			//year = "2015";
			startTime = year + "-01";
			endTime = year + "-12";
		}
		
		// 将 String 类型的开始时间和结束时间
		// 转换为 Date 类型
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date startMonthDate = new Date();
		Date endMonthDate = new Date();
		try {
			startMonthDate = sdf.parse(startTime);
			endMonthDate = sdf.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		List<String> internList = zEmployeeService.getEntryTypeList(departmentId, 2, startMonthDate, endMonthDate);
		Map<String, Object> internMap = new HashMap<String,Object>();
		internMap.put("name", "实习");
		internMap.put("type", "bar");
		internMap.put("stack", "员工入职类型");
		internMap.put("data", internList.toArray());
		
		List<String> otherList = zEmployeeService.getEntryTypeList(departmentId, 1, startMonthDate, endMonthDate);
		Map<String, Object> otherMap = new HashMap<String,Object>();
		otherMap.put("name", "正式");
		otherMap.put("type", "bar");
		otherMap.put("stack", "员工入职类型");
		otherMap.put("data", otherList.toArray());

		Map<String, Object> entryTypeMap = new HashMap<String, Object>();		
		List<String> titleList = new ArrayList<String>();
		titleList.add("实习");
		titleList.add("正式");
		entryTypeMap.put("title", titleList);
		entryTypeMap.put("name1", "人数");		
		entryTypeMap.put("dates", getMonthList(startMonthDate, endMonthDate));
		
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataList.add(internMap);
		dataList.add(otherMap);
		entryTypeMap.put("data", dataList);
		
		if (departmentId == 1) {
			entryTypeMap.put("department", "东方国信");
		} else {
			String departmentName = zEmployeeService.getDeptName(departmentId);
			entryTypeMap.put("department", departmentName);
		}
		return JSON.toJSONString(entryTypeMap);
	}
	
	
	/**
	 * HC周报
	 * departmentId 必传
	 * departmentId 是登录用户所在的一级部门
	 * @param departmentId
	 * @param startDay
	 * @param endDay
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/HCWeek")
	public String getHCWeek(Integer departmentId, String startTime, String endTime){
			
		/**
		 * 起止时间和结束时间任一为空
		 * 则默认显示近三个月的数据
		 */
		if (departmentId == null) {
			departmentId = 1;
		}
		if( null == startTime || "".equals(startTime) || null == endTime || "".equals(endTime)) {		
			
			/**
			 * 获取当前年、月、日
			 */
			Calendar nowCal = Calendar.getInstance();
			String nowYear = String.valueOf(nowCal.get(Calendar.YEAR));
			String nowMonth = String.valueOf(nowCal.get(Calendar.MONTH) + 1);	
			String nowDate = String.valueOf(nowCal.get(Calendar.DATE));
			//System.out.println("今天的日期：" + nowYear + "年" + nowMonth + "月" + nowDate + "");
			
			/**
			 * 获取三个月前的年、月、日
			 */
			Calendar startCal = Calendar.getInstance();
			startCal.add(Calendar.MONTH, -2);
			String startYear = String.valueOf(startCal.get(Calendar.YEAR));
			String startMonth = String.valueOf(startCal.get(Calendar.MONTH) + 1);
			String startDate= String.valueOf(startCal.get(Calendar.DATE));
			//System.out.println("三个月前的日期：" + startYear + "年" + startMonth + "月" + startDate + "");
			
			/**
			 * 拼凑成 年-月-日
			 */
			startTime= startYear + "-" + startMonth + "-" + startDate;
			endTime = nowYear + "-" + nowMonth + "-" + nowDate;
			}
		//------end if ------//
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date stratDate = new Date();
		Date endDate = new Date();
		try {
			stratDate = sdf.parse(startTime);
			endDate = sdf.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		List<String> internList = 
				zEmployeeService.getEntryTypeNumByWeek(departmentId, 2, stratDate, endDate);
		Map<String, Object> internMap = new HashMap<String,Object>();
		internMap.put("name", "实习");
		internMap.put("type", "bar");
		internMap.put("stack", "员工入职类型");
		internMap.put("data", internList.toArray());
		
		List<String> otherList = 
				zEmployeeService.getEntryTypeNumByWeek(departmentId, 1, stratDate, endDate);
		Map<String, Object> otherMap = new HashMap<String,Object>();
		otherMap.put("name", "正式");
		otherMap.put("type", "bar");
		otherMap.put("stack", "员工入职类型");
		otherMap.put("data", otherList.toArray());

		Map<String, Object> entryTypeByWeekMap = new HashMap<String, Object>();		
		List<String> titleList = new ArrayList<String>();
		titleList.add("实习");
		titleList.add("正式");
		entryTypeByWeekMap.put("title", titleList);
		entryTypeByWeekMap.put("name1", "人数");
		entryTypeByWeekMap.put("dates", getFridayList(stratDate,endDate));
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataList.add(internMap);
		dataList.add(otherMap);
		entryTypeByWeekMap.put("data", dataList);
		if (departmentId == null || departmentId.equals("")) {
			entryTypeByWeekMap.put("department", "东方国信");
		} else {
			String departmentName = zEmployeeService.getDeptName(departmentId);
			entryTypeByWeekMap.put("department", departmentName);
		}
		//System.out.println("实习生人数列表"+internList);
		//System.out.println("正式人数列表"+otherList);
		//System.out.println("departmentId"+departmentId);
		return JSON.toJSONString(entryTypeByWeekMap);
	}


	
	/**
	 * 短期离职员工按性别、部门分布
	 * @param departmentId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/shortDimissionByGender")
	public String getShortDimissionByGender(Integer departmentId){
		
		if(departmentId == null){
			departmentId =1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getSDDByGender(departmentId);
		map.put("title","性别分布");
		map.put("gender", list);
		if (departmentId == 1) {
			map.put("department", "东方国信");
		} else {
			String departmentName = zEmployeeService.getDeptName(departmentId);
			map.put("department", departmentName);
		}
		return JSON.toJSONString(map);
	}
	
	/**
	 * 短期离职员工按年龄分布
	 * @param 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/shortDimissionByAge")
	public String getShortDimissionByAge(Integer departmentId){
		
		if (departmentId == null) {
			departmentId = 1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getSDDByAge(departmentId);
		map.put("title", "年龄分布");
		map.put("age", list);
		if (departmentId == 1) {
			map.put("department", "东方国信");
		} else {
			String departmentName = zEmployeeService.getDeptName(departmentId);
			map.put("department", departmentName);
		}
		return JSON.toJSONString(map);
	}

	/**
	 * 短期离职员工按学历分布
	 * @param departmentId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/shortDimissionByDegree")
	public String getShortDimissionByDegree(Integer departmentId){
		
		if (departmentId == null) {
			departmentId = 1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getSDDByDegree(departmentId);
		map.put("title", "学历分布");
		map.put("degree", list);
		return JSON.toJSONString(map);
	}
	
	/**
	 * 短期离职员工按工作年限(WorkExperience)分布
	 * @param departmentId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/shortDimissionByWE")
	public String getShortDimissionByWE(Integer departmentId){
		
		if (departmentId == null) {
			departmentId = 1;
		}
		Map<String, Object> map = new HashMap<String,Object>();
		List<Template> list = new LinkedList<>();
		list = zEmployeeService.getSDDByWE(departmentId);
		map.put("title", "工作年限分布");
		map.put("workExperience", list);
		return JSON.toJSONString(map);	
	}
	
	/**
	 * 短期离职员工仅按部门(Dept)分布
	 * 取离职人员数量前十的部门
	 */
	@ResponseBody
	@RequestMapping(value = "shortDimissionByDept")
	public String getShortDimissionByDept(Integer departmentId){
			
		Map<String, Object> map = new HashMap<String,Object>();
		Map<String, List<String>> rsMap = new HashMap<String, List<String>>();
		List<String> topDeptStrList = new ArrayList<>();
		List<Integer> topNumIntList = new ArrayList<>();
		
		if (departmentId == null || departmentId == 1) {
			rsMap = zEmployeeService.getShortDimissionTOPDept();
			topDeptStrList = rsMap.get("name");
			for (int i = 0; i < rsMap.get("num").size(); i++) {
				try {
					Integer num = new Integer(rsMap.get("num").get(i));
					topNumIntList.add(num);
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
			}
		}
		else{
			if (zEmployeeService.getSubDeptNum(departmentId) == 0) {
				for (int i = 0; i <10; i++) {
					topDeptStrList.add("无");
					topNumIntList.add(0);					
				}
				
			}else {
				rsMap = zEmployeeService.getShortDimissionTOPDeptByDept(departmentId);
				topDeptStrList = rsMap.get("name");
				for (int i = 0; i < rsMap.get("num").size(); i++) {
					try {
						Integer num = new Integer(rsMap.get("num").get(i));
						topNumIntList.add(num);
					} catch (NumberFormatException e) {
						e.printStackTrace();
					}
				}
			}

		}
				
		//柱状图添加标题 -- 短期离职前十部门
		List<String> titleList = new ArrayList<>();
		titleList.add("短期离职前十部门");
		map.put("title", titleList);
		
		//柱状图添加横坐标 -- 前十部门名称
		map.put("dates", topDeptStrList);
		
		//柱状图添加竖坐标 -- 各部门短期离职数
		Map<String, Object> dataMap = new HashMap<String,Object>();
		dataMap.put("name", "短期离职前十部门");
		dataMap.put("type", "bar");
		dataMap.put("barWidth", 50);
		dataMap.put("data", topNumIntList.toArray());
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataList.add(dataMap);
		map.put("data", dataList.toArray());
		
		//柱状图添加竖坐标单位 -- "人数"
		map.put("name1", "人数");
		
		return JSON.toJSONString(map);
	}
	
	/**
	 * test
	 */
	@ResponseBody
	@RequestMapping(value = "/test")
	public List<String> test(){
		
		/**

		Date friday = new Date();
		Calendar nextFriday = Calendar.getInstance();
		nextFriday.setTime(friday);
		nextFriday.add(Calendar.DATE, -6);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");		
		return sdf.format(nextFriday.getTime());
		 * 
		 */
		
		/**
		 * 获取周五列表
		 */
		String str1 = "2017-01-01";
		String str2 = "2017-02-08";
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date startMonthDate = new Date();
		Date endMonthDate = new Date();
		try {
			startMonthDate = sdf.parse(str1);
			endMonthDate = sdf.parse(str2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startMonthDate);		
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endMonthDate);
		
		
		List<String> fridayList = new ArrayList<String>();
		while(!endCal.equals(startCal)) {			
			if(startCal.get(Calendar.DAY_OF_WEEK) == 6 ){
				fridayList.add(sdf.format(startCal.getTime()));				
			}
			startCal.add(Calendar.DATE, 1);
		}
		if (startCal.get(Calendar.DAY_OF_WEEK) == 6) {
			fridayList.add(sdf.format(startCal.getTime()));
		}
		
		
		/**
		 * 获取周六
		 */
		List<String> lastSaturdayList = new ArrayList<>();
		for (int i = 0; i < fridayList.size(); i++) {
			// String -> Date
			String nextFridayString = fridayList.get(i);
			Date nextFridayDate = new Date();
			try {
				nextFridayDate = sdf.parse(nextFridayString);
			}catch (ParseException e) {
				e.printStackTrace();
			}			
			Calendar nextFridayCal = Calendar.getInstance();
			nextFridayCal.setTime(nextFridayDate);
			nextFridayCal.add(Calendar.DATE, -6);
			lastSaturdayList.add(sdf.format(nextFridayCal.getTime()));
		}	
		return lastSaturdayList;

	}
/**
 * 获取所选日起范围内的所有周五
 * 星期日：
 * @param startDate
 * @param endDate
 * @return
 */
	private List<String> getFridayList(Date startDate, Date endDate){
		
		Calendar startCal = Calendar.getInstance();
		startCal.setTime(startDate);		
		Calendar endCal = Calendar.getInstance();
		endCal.setTime(endDate);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		List<String> fridayList = new ArrayList<String>();
		while(!endCal.equals(startCal)) {			
			if(startCal.get(Calendar.DAY_OF_WEEK) == 6 ){
				fridayList.add(sdf.format(startCal.getTime()));				
			}
			startCal.add(Calendar.DATE, 1);
		}
		if (startCal.get(Calendar.DAY_OF_WEEK) == 6) {
			fridayList.add(sdf.format(startCal.getTime()));
		}
		return fridayList;
	}
	
/**
 * 获取所选日期范围内的所有月份列表
 * @param startMonth
 * @param endMonth
 * @return
 */
	private List<String> getMonthList(Date startMonth, Date endMonth) {
		List<String> monthList = new ArrayList<String>();
		
		Calendar startMonthCal = Calendar.getInstance();
		startMonthCal.setTime(startMonth);
		
		Calendar endMonthCal = Calendar.getInstance();
		endMonthCal.setTime(endMonth);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		while(!endMonthCal.equals(startMonthCal)) {
			monthList.add(sdf.format(startMonthCal.getTime()));
			startMonthCal.add(Calendar.MONTH, 1);
		}
		monthList.add(sdf.format(startMonthCal.getTime()));
		
		return monthList;
	}
}



